#ifndef PAQUETEHASHRECEPTOR_H_INCLUDED
#define PAQUETEHASHRECEPTOR_H_INCLUDED

#include <string>

#define END "@"
class PaqueteHashEmisor;

class PaqueteHashReceptor{
    private:
    std::string MD5;
    std::string ROLLINGCS;
    unsigned int NumPaquete;

    public:

    PaqueteHashReceptor( unsigned int NumP, std::string& md5 , std::string& rollingCS )
    {
        this->MD5 = md5;
        this->ROLLINGCS = rollingCS;
        this->NumPaquete = NumP;
    }

    PaqueteHashReceptor(const std::string& Bytes)
    {
        this->Hidratar(Bytes);
    }

    ~PaqueteHashReceptor(){}

    unsigned int getNumPaquete(){return this->NumPaquete;}



    void Hidratar(const std::string& str)
    {
        std::stringstream SS(str);
        unsigned int LongMD5 = 0;
        SS >> LongMD5;

        SS.ignore(1,EOF);

        char* buffer = new char[LongMD5+1];

        buffer[LongMD5]='\0';
        SS.read(buffer,LongMD5);
        std::string md5(buffer);

        delete [] buffer;

        this->MD5 = md5 ;
        this->MD5.erase(LongMD5,1);
        unsigned int LongAd32 = 0;
        SS >> LongAd32;
        SS.ignore(1,EOF);

        char* buf = new char[LongAd32+1];

        buf[LongAd32]='\0';
        SS.read(buf,LongAd32);
        std::string ad32(buf);

        delete [] buf;

        this->ROLLINGCS = ad32;
        this->MD5.erase(LongMD5,1);
        SS.ignore(1,EOF);
        SS >> this->NumPaquete;
        /*
        std::cout << LongMD5 << " "<< this->MD5
        << " " << LongAd32 << " " << this->ROLLINGCS
        << " " << this->NumPaquete << std::endl;
        */
    }

    std::string Serializar()
    {
        std::string Bytes;
        std::stringstream SS("");

        SS << this->MD5.size();
        SS >> Bytes;
        Bytes.append(" ");
        Bytes.append( this->MD5 );
        Bytes.append(" ");
        SS.clear();

        std::string aux;
        SS << this->ROLLINGCS.size();
        SS >> aux;
        Bytes.append(aux);
        Bytes.append(" ");
        Bytes.append( this->ROLLINGCS );
        Bytes.append(" ");

        SS.clear();

        SS << this->NumPaquete;
        SS >> aux;
        Bytes.append(aux);
        Bytes.append(END);
        //std::cout << Bytes << std::endl;
        return Bytes;
    }

    friend class PaqueteHashEmisor;

};

#endif // PAQUETEDEHASHES_H_INCLUDED
